import { http, store, constant } from 'aoyun-base'

const APIENV = constant.APIENV
// prod正式 tst测试 dev开发

const message = {
    toast: function (message) {
        //  todo
        Toast(message)
    },
    top: function (message) {
        //  todo
    }
}
const conf = {
    //  none、debug、error
    // level: 'debug',
    whiteList:['baiduaip/rest'],
    query: {},
    onStart: function (config) {
        //  todo loading stop
        return config
    },
    onComplete: function () {
        //  todo loading stop
    },
    parseError: function (error) {
        //  todo 解析已知异常
        let err = new Error(error.data.message.info)
        err.code = error.data.message.code
        err.name = http.exception.CLIENT_ERROR
        return err
    },
    onTokenError: function (error) {
        //  todo 处理token异常，rediect to login
        location.href = location.origin + '/login/login.html?access_token='
        //  code: 401
    },
    onTimeOutError: function (error) {
        //  todo 处理服务端异常，toast
        //  code: 0
        message.toast(error.message)
    },
    onClientError: function (error) {
        //  todo 处理客户端提交的请求异常，top
        //  code: 200
        message.top(error.message)
    },
    onServiceError: function (error) {
        //  todo 处理服务端异常，toast
        //  code: 500
        message.toast(error.message)
    },
    onError: function (error) {
        //  name: exception.UN_KNOW_ERROR
        //  code: 200/400
        console.error('onError,code:' + error.code, error)
    }
}

http.init(conf)

const exception = {
    custom: function (message) {
        let err = new Error(message)
        err.code = 0
        err.name = http.exception.CUSTOM_ERROR
        return err
    }
}

export {
    http,
    exception,
    APIENV
}
